import java.util.ArrayList;
import java.util.List;

public class Test {
    public List<Integer> findAnagrams(String s, String p) {
        int hash1[] = new int[26];
        int hash2[] = new int[26];
        List<Integer> ret = new ArrayList<>();
        int n = p.length();
        for(int i = 0;i<p.length();i++) {
            char ch = p.charAt(i);
            hash1[ch-'a']++;
        }
        int count = 0;
        for(int left = 0,right = 0; right<s.length(); right++) {

            char ch = s.charAt(right);
            hash2[ch-'a']++;
            if(hash2[ch-'a']<=hash1[ch-'a']) {
                count++;
            }
            if(right-left+1>n) {
                char out = s.charAt(left);
                if(hash2[out-'a']--<=hash1[out-'a']) {
                    count--;
                }
                left++;
            }
            if(count==n) {
                ret.add(left);
            }
        }
        return ret;
    }

    public static void main(String[] args) {
        Test test = new Test();
        test.findAnagrams("cbaebabacd","abc");
    }
}
